package com.njtoyo.taxi.admin.rest.controller.business.geo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.mapper.master.entity.GeoArea;
import com.njtoyo.taxi.admin.rest.presenter.business.geo.AllListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.area.AreaCreateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.area.AreaQueryWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.area.AreaUpdateIsOpenWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.area.AreaUpdateWrapper;
import com.njtoyo.taxi.admin.service.business.geo.AreaService;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value = "地区", tags = "地区")
@RequestMapping("business/geo/area")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @GetMapping("/all")
    @ApiOperation(value = "全部地区", notes = "无接口权限校验")
    RestResult<List<AllListPresenter>> getAll() {
        return areaService.getAll();
    }

    @GetMapping()
    @ApiOperation(value = "地区列表", notes = "地区列表：权限：area_list")
    @PermissionScope(names = {"area_list"})
    RestResult<IPage<GeoArea>> getList(AreaQueryWrapper wrapper) {
        return areaService.getList(wrapper);
    }

    @PatchMapping("/{id}")
    @ApiOperation(value = "修改地区", notes = "权限：area_update")
    @PermissionScope(names = {"area_update"})
    RestResult<Boolean> update(@PathVariable Long id, @RequestBody AreaUpdateWrapper wrapper) {
        return areaService.update(id, wrapper);
    }

    @PatchMapping("/is_open")
    @ApiOperation(value = "修改地区是否开通", notes = "权限：area_update")
    @PermissionScope(names = {"area_update"})
    RestResult<Boolean> updateIsOpen(@Valid @RequestBody AreaUpdateIsOpenWrapper wrapper) {
        return areaService.updateIsOpen(wrapper);
    }

    @PostMapping()
    @ApiOperation(value = "创建地区", notes = "创建地区，权限：area_create")
    @PermissionScope(names = {"area_create"})
    RestResult create(@Valid @RequestBody AreaCreateWrapper wrapper) {
        return areaService.create(wrapper);
    }

    @GetMapping("refresh")
    RestResult<?> refresh() {
        return areaService.refresh();
    }
}
